package com.hefeng.wx.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hefeng.common.core.web.domain.AjaxResult;
import com.hefeng.wx.controller.entity.ParamEntity;
import com.hefeng.wx.domain.WxDevice;
import com.hefeng.wx.domain.WxDeviceInfo;
import com.hefeng.wx.feign.GascollectionClient;
import com.hefeng.wx.feign.TestFeign;
import com.hefeng.wx.service.GasDeviceInfoService;
import com.hefeng.wx.service.WxDeviceInfoService;
import com.hefeng.wx.service.WxDeviceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.hefeng.common.core.utils.EmptyUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/resident/device")
@Api(tags="设备信息表对象功能接口")
public class WxDeviceController {
    @Autowired
    WxDeviceService wxDeviceService;
    @Autowired
    WxDeviceInfoService wxDeviceInfoService;
    @Autowired
    GascollectionClient gascollectionClient;
    @Autowired
    GasDeviceInfoService gasDeviceInfoService;
    @Autowired
    TestFeign testFeign;
    /**
     * 方法说明: 获取用户所有设备列表
     * @Param: [userLogin]
     * @return: io.renren.util.ResultUtil
     * @Author: huzh
     * @Date: 2022/2/16
     */
    @RequestMapping(value = "/getDeviceList",produces = "application/json;charset=UTF-8")
    @ApiOperation("获取用户所有设备列表")
    public AjaxResult getDeviceList(@RequestBody ParamEntity paramEntity){
        Map<String,Object> data=new HashMap<String,Object>();
        List<Map> gasList=wxDeviceService.getDeviceList(paramEntity.getUserId(),paramEntity.getPage(),paramEntity.getPageSize());
        data.put("gasList",gasList);
        return AjaxResult.success(gasList);
    }


    /**
     * 方法说明: 保存设备
     * @Param: [userLogin]
     * @return: io.renren.util.ResultUtil
     * @Author: huzh
     * @Date: 2022/2/16
     */
    @RequestMapping(value = "/saveDevice",produces = "application/json;charset=UTF-8")
    @ApiOperation("保存设备")
    public AjaxResult saveDevice(@RequestBody WxDevice wxDevice){
        Map<String, Object> data=wxDeviceService.insert(wxDevice);
        if(!data.get("make").equals(true)){
            return AjaxResult.error(501,data.get("msg").toString());
        }
        return AjaxResult.success();
    }

    /**
     * 方法说明: 修改设备
     * @Param: [userLogin]
     * @return: io.renren.util.ResultUtil
     * @Author: huzh
     * @Date: 2022/2/16
     */
    @RequestMapping(value = "/updateDevice",produces = "application/json;charset=UTF-8")
    @ApiOperation("修改设备")
    public AjaxResult updateDevice(@RequestBody WxDevice wxDevice){
        Map<String, Object> data=wxDeviceService.update(wxDevice);
        if(!data.get("make").equals(true)){
            return AjaxResult.error(501,data.get("msg").toString());
        }
        return AjaxResult.success("修改成功！",data);
    }
    /**
     * 方法说明: 获取设备详情
     * @param paramEntity
     * @return null
     * @Author: huzh
     * @Date: 2022/9/16
     */
    @RequestMapping(value = "/detailsDevice",produces = "application/json;charset=UTF-8")
    @ApiOperation("获取设备详情")
    public AjaxResult detailsDevice(@RequestBody ParamEntity paramEntity){
        Map<String,Object> dataMap=new HashMap<>();
        if (EmptyUtils.isEmpty(paramEntity.getUserId())){
            return AjaxResult.error(500,"用户未登陆");
        }
        WxDevice device=wxDeviceService.getById(paramEntity.getDeviceId());
        LambdaQueryWrapper<WxDeviceInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WxDeviceInfo::getManufacturerCode,device.getManufacturerCode());
        WxDeviceInfo deviceInfo=wxDeviceInfoService.getOne(queryWrapper);
        dataMap.put("device",device);
        dataMap.put("deviceInfo",deviceInfo);
        return AjaxResult.success("获取成功",dataMap);
    }
    /**
     * 方法说明:  删除设备
     * @param paramEntity
     * @return null
     * @Author: huzh
     * @Date: 2022/9/19
     */
    @RequestMapping(value = "/deleteDevice",produces = "application/json;charset=UTF-8")
    @ApiOperation("获取设备详情")
    public  AjaxResult deleteDevice(@RequestBody ParamEntity paramEntity){
        Map<String,Object> dataMap=new HashMap<>();
        dataMap=wxDeviceService.deleteDevice(paramEntity.getDeviceId());
        if(!dataMap.get("make").equals(true)){
            return AjaxResult.error(501,dataMap.get("msg").toString());
        }
        return AjaxResult.success("删除成功！");
    }

}
